package org.mozhu.mboot.admin.front.web.controller;

import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;
import org.mozhu.mboot.admin.system.account.entity.User;
import org.mozhu.mboot.admin.system.account.service.UserService;
import org.mozhu.mboot.core.config.ApplicationConfiguration;
import org.mozhu.mboot.core.json.JSON;
import org.mozhu.mboot.core.web.AjaxResponse;
import org.mozhu.mboot.core.web.RequestContextHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Enumeration;
import java.util.Map;

@Controller
@RequestMapping("/dev")
@Slf4j
public class DevController {

    @Value("${profile}")
    private String profile;

    @Autowired
    UserService userService;

    @RequestMapping("/profile")
    @ResponseBody
    public String profile() {
        return profile;
    }

    @RequestMapping("/profile2")
    @ResponseBody
    public String profile2() {
        String profile = ApplicationConfiguration.get().getProperty("profile");
        return profile;
    }

    @RequestMapping("save/user")
    @ResponseBody
    public Object saveUser(@RequestParam("name") String name) {
        User user = new User();
        user.setLoginName(name);
        user.setEmail(name + "@qq.com");
        user.setPlainPassword("123456");
        userService.encryptPassword(user);
        userService.save(user);
        return user;
    }

    @RequestMapping("/{view}")
    public String page(Model model, @PathVariable("view") String view) {
        return "dev/" + view;
    }


    @GetMapping("/session")
    public String session(Model model) {
        HttpServletRequest request = RequestContextHolder.getRequest();
        HttpSession session = request.getSession();
        String sessionId = session.getId();
        log.info("Session Id [{}]", sessionId);
        Map<String, String> sessionAttributes = Maps.newHashMap();
        Enumeration<String> attributeNames = session.getAttributeNames();
        while (attributeNames.hasMoreElements()) {
            String attributeKey = attributeNames.nextElement();
            Object attributeValue = session.getAttribute(attributeKey);
            sessionAttributes.put(attributeKey, JSON.toPrettyJson(attributeValue));
        }

        model.addAttribute("sessionAttributes", sessionAttributes);
        model.addAttribute("sessionId", sessionId);
        return "dev/session";
    }

    @PostMapping("/session")
    public String sessionUpdate(@RequestParam("key") String key, @RequestParam("value") String value) {
        HttpServletRequest request = RequestContextHolder.getRequest();
        HttpSession session = request.getSession();
        String id = session.getId();
        log.info("Session Id [{}]", id);
        session.setAttribute(key, value);
        return "redirect:/dev/session";
    }

    @PostMapping("/session/delete")
    @ResponseBody
    public Object sessionDelete(@RequestParam("key") String key) {
        HttpServletRequest request = RequestContextHolder.getRequest();
        HttpSession session = request.getSession();
        String id = session.getId();
        log.info("Session Id [{}]", id);
        session.removeAttribute(key);
        return AjaxResponse.success();
    }

    public void setProfile(String profile) {
        this.profile = profile;
    }
}
